<script type="text/javascript">
    function commit()
    {
        var faction = document.getElementById("faction");
        var form = document.getElementById("fr");
        if (faction.value == 'add')
            form.action += 'add';
        else
            form.action += 'edit/' <?php if (isset($id)) echo "+ ".$id; else echo ""; ?>;
        form.submit();
    }
</script>
<fieldset>
    <legend>Thông tin bài viết</legend>
    <form action="<?php echo Utility::getSiteUrl();?>/post/" method="post" id="fr">
        <span id="message" style="color:red;font-weight:bold;background-color:#f0f8ff;">
        <?php
         if(isset($error))
         {
            foreach($error as $err)
            {
                echo "* $err <br />";
            }
         }

        if(isset($message))
        {
            echo "* $message <br/>";
        }

        if(isset($inserted_id))
        {
            global $base_url;
            $link = $base_url . '/news/show/' . $inserted_id;
            echo "* Xem bài viết tại : <a href='$link'>$link</a> <br/>";
        }
        echo "<br/>";
        ?>
    </span>
        <input type="hidden" name="faction" id="faction" value="<?php echo $action; ?>" />
        <table>
            <tr>
                <td>
                    Tiêu đề<br />
                    <input type="text" style="width:300px;" name="title" value="<?php  if(isset($input)) if($action == 'edit') echo $input->title; else echo $input['title'];?>"/></td>
            </tr>
            <tr>
                <td>
                    Tóm tắt<br />
                    <textarea rows="5" cols="50" name="summary" style="width:300px;"><?php  if(isset($input)) if($action == 'edit') echo $input->
                    summary; else echo $input['summary'];?></textarea>
                </td>
            </tr>
            <tr>
                <td>
                    Nội dung bài viết<br />
                    <textarea rows="5" cols="50" name="detail" id="detail"><?php  if(isset($input)) if($action == 'edit') echo $input->
                    detail; else echo $input['detail'];?></textarea>
                </td>
            </tr>
            <script type="text/javascript">
                CKEDITOR.replace('detail');
            </script>
            <tr>
                <td>
                    <input
                        type="button" value="thêm ảnh"
                        onclick="window.open('<?php echo Utility::getSiteUrl(); ?>/upload ','Upload','menubar=no,width=800,height=500,toolbar=no')"/>
                    <br/>
                    Ảnh đại diện<br />
                    <?php
                      if(isset($input))
                      {
                      ?>
                        <img src="<?php if($action == 'edit') echo $input->avatar; else echo $input['avatar'];?>" style="max-width:200px"/>
                        <br/>
                        <?php
                        }
                      ?>
                    <input type="text" name="avatar" style="width:300px;" value="<?php  if(isset($input)) if($action == 'edit') echo $input->avatar; else echo $input['avatar']; ?>"/>
                </td>
            </tr>
            <tr>
                <td>
                    Danh mục<br />
                    <select name="category_id" style="width:305px;">
                        <option value="0">Chọn danh mục</option>
                        <?php
                        if(isset($cat_list))
                        {
                            if($action == 'edit')
                            {
                                foreach($cat_list as $cat)
                                {
                                    if(isset($id) && $id == $cat->id) continue;
                                    echo "<option value='$cat->id'";
                                    if(isset($input) && $input->category_id == $cat->id) echo "selected='selected'";
                                    echo ">";
                                    for($i = 0; $i < $cat->c_level; $i++) echo "--";
                                    echo " $cat->name</option>";
                                }
                            }
                            else
                            {
                                foreach($cat_list as $cat)
                                {
                                    if(isset($id) && $id == $cat->id) continue;
                                    echo "<option value='$cat->id'";
                                    if(isset($input) && $input['category_id'] == $cat->id) echo "selected='selected'";
                                    echo ">";
                                    for($i = 0; $i < $cat->c_level; $i++) echo "--";
                                    echo " $cat->name</option>";
                                }
                            }
                        }
                        ?>
                    </select>
                </td>
            </tr>
            <tr>

                <td>
                    Trạng thái<br />
                    <select name="status" style="width:305px;">
                        <option value="1" <?php if(isset($input)) if($action == 'edit') {if($input->status == 1) echo "selected='selected'";} else {if($input['status'] == 1)
                        echo "selected='selected'";} ?> >Hiển thị</option>
                        <option value="0"
                        <?php if(isset($input)) if($action == 'edit') {if($input->status == 0) echo
                        "selected='selected'";} else {if($input['status'] == 0)
                        echo "selected='selected'";} ?>>Không hiển thị</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    Cho phép bình luận ?<br />
                    <select name="comment_enable" style="width:305px;">
                        <option value="1"
                        <?php if(isset($input)) if($action == 'edit') {if($input->comment_enable == 1) echo
                        "selected='selected'";} else {if($input['comment_enable'] == 1)
                        echo "selected='selected'";} ?> >Có</option>
                        <option value="0"
                        <?php if(isset($input)) if($action == 'edit') {if($input->comment_enable == 0) echo
                        "selected='selected'";} else {if($input['comment_enable'] == 0)
                        echo "selected='selected'";} ?>>Không</option>
                    </select>
                </td>
            </tr>
            <tr><td>&nbsp;</td></tr>
            <tr>
                <td><input type="button" value="<?php if($action =='add') echo "Thêm"; else echo "Sửa";?>" onclick="commit()">&nbsp;&nbsp;<input type="reset" value="Nhập lại"></td>
            </tr>
        </table>
    </form>
</fieldset>